package onetomanymapping;

import java.util.Iterator;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class OneToManyProgram {
	public static void main(String[] args) {
		System.out.println("one to many : Autor -> Books");
		
		SessionFactory sessions=new AnnotationConfiguration().
				configure("one_to_many_Book_Autor_hibernate.cfg.xml").buildSessionFactory();
		
		Session session = sessions.openSession();
		session.getTransaction().begin();
		
		Autor karliVatson=(Autor) session.get(Autor.class, 3);
		System.out.println(karliVatson.getAutor_name_and_id_concatenated());
		
		for (Book item : karliVatson.getBooks()) {
			System.out.println(item.getBook_title());
		}
		
		//remove autor , and all his books 
		//session.delete(karliVatson);
		
		session.getTransaction().commit();
	}
}